import axios from 'axios'
import { Base64 } from 'js-base64';
import { ElMessageBox } from 'element-plus'


let instance = axios.create({
    responseType: 'json',
    headers: { 'content-Type': 'application / json' }
})


function baseFun() {
    const token = localStorage.getItem('token')
    const base64 = Base64.encode(token + ":")
    return 'Basic' + base64
}


instance.interce     
ptors.request.use(
    config => {
        let token = localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = baseFun()

        }
        return config
    },
    err => {

        return Promise.reject(err)
    }
)

instance.interceptors.response.use(
    response => {
        return response
    },
    error => {
        if (error.response) {
            let ERRS = error.response.status
            let MSG = error.response.data.msg.msg
            let errdata = ERRS == 401 ? MSG : '服务器发生错误'
            switch (ERRS) {
                case 401:
            ElMessageBox.alert(errdata, '提示', {
                confirmButtonText: '好的',
                type: 'error'
            })
            .catch(res => {

            })
            .catch(err => {

            })
            break;
            
        }

    }
    return Promise.reject(error.response.data)

   }
)

export default instance